//
//  ViewController.m
//  ImageViewer
//
//  Created by shenchengcai on 15/2/11.
//  Copyright (c) 2015年 QM. All rights reserved.
//

#import "RWViewController.h"
#import <RWUIControls/RWUIControls.h>
#import "LoginManager.h"

@interface RWViewController ()
@property (nonatomic, strong) RWScrollView *_rwTable;
@end

@implementation RWViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    RWScrollView *testTable = [[RWScrollView alloc] initWithFrame:CGRectMake(20, 50, 280, 300)];
//    testTable.backgroundColor = [UIColor redColor];
    [self.view addSubview:testTable];
    [testTable addHeaderWithTarget:self action:@selector(searchList)];
    __rwTable = testTable;
}

//搜索下载列表
-(void)searchList{
    NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
    [dic setObject:@"admin5@126.com" forKey:@"username"];
    [dic setObject:@"5" forKey:@"authority_id"];
    [dic setObject:@"27" forKey:@"number"];
    [dic setObject:@"1" forKey:@"page"];
    [dic setObject:@"0" forKey:@"lastbook_id"];
    [dic setObject:@"0" forKey:@"downloaded_book_ids"];
    [dic setObject:@"" forKey:@"key_word"];
    [dic setObject:@"" forKey:@"book_year"];
    [dic setObject:@"" forKey:@"book_type"];
    
    [LoadingHelper showLoadingWithView:self.view];
    
    [LoginManager getDownloadListWithOption:dic andBlock:^(NSMutableArray *collection, NSError *error) {
        
        [LoadingHelper hiddonLoadingWithView:self.view];
        
        // (最好在刷新表格后调用)调用endRefreshing可以结束刷新状态
        [__rwTable headerEndRefreshing];
        
        //从接口获取电子书列表不为空
        if (!error){//collection.count != 0) {
            NSLog(@"collection:%@",collection);
            /*
             //下拉刷新
             if (_refreshSys==1) {
             _refreshSys = 2;
             _arrdata = [[NSMutableArray alloc]init];
             // (最好在刷新表格后调用)调用endRefreshing可以结束刷新状态
             [_tableViewShow headerEndRefreshing];
             }
             //上拉加载
             else {
             _refreshSys = 2;
             // (最好在刷新表格后调用)调用endRefreshing可以结束刷新状态
             [_tableViewShow footerEndRefreshing];
             }
             
             _text++;
             //            _clickNot = 0;
             //数据
             _arrText = [[NSMutableArray alloc]initWithArray:collection];
             [_arrdata addObject:_arrText];
             [_tableViewShow reloadData];
             
             NSString * str = [NSString stringWithFormat:@"DELETE FROM downloadBook"];
             [DBDaoHelper deleteTableData:str];
             for (int j = 0; j < _arrdata.count; j++) {
             for (int i = 0; i < [_arrdata[j] count]; i++) {
             ShowListModel * book =  [[ShowListModel alloc]init];
             book = _arrdata[j][i];
             NSString *sql1 = [NSString stringWithFormat:
             @"INSERT INTO 'downloadBook' ('book_id','title','content' ,'time' ,'imgurl','book_type') VALUES (%@,'%@','%@','%@','%@','%@')",
             book.book_id ,book.title,book.content,book.time,book.imgurl,book.book_type];
             [DBDaoHelper insertTypeWithSql:sql1];
             }
             }
             if (_arrText.count == perNumber.intValue){
             [_tableViewShow addFooterWithTarget:self action:@selector(newRereshing)];
             }
             else {
             [_tableViewShow removeFooter];
             }
             */
        }
        /*
         //从接口获取电子书列表为空，显示列表为空
         else if(_text == 0){
         //            _clickNot = 1;
         _text++;
         _arrdata = [[NSMutableArray alloc]init];
         //从本地缓存加载数据
         [_arrdata addObject:[DBDaoHelper selectTypeWithsql1:nil]];
         [_tableViewShow reloadData];
         //下拉刷新
         if (_refreshSys==1) {
         _refreshSys = 2;
         // (最好在刷新表格后调用)调用endRefreshing可以结束刷新状态
         [_tableViewShow headerEndRefreshing];
         }
         }
         //从接口获取电子书列表为空，显示列表不为空，结束刷新状态
         else if (_arrdata.count > 0) {
         //下拉刷新
         if (_refreshSys==1) {
         
         [TCUtils showAlertViewWithTitle:nil message:@"网络连接超时，请检查下载URL" delegate:self cancel:nil other:@"确定" tag : 102];
         }
         //上拉加载
         else {
         
         [TCUtils showAlertViewWithTitle:nil message:@"网络连接超时，请检查下载URL" delegate:self cancel:nil other:@"确定" tag : 103];
         }
         }
         */
        //没有搜索到符合条件的数据
        else {
            //显示当前页面时
            //            if (self.pagesContainer.selectedIndex == 2 && [[((UINavigationController *)self.navigationController).viewControllers lastObject] isKindOfClass:[DownloadViewController class]]) {
            [TCUtils showAlertViewWithTitle:nil message:@"没有搜索到符合条件的数据" delegate:self cancel:nil other:@"确定" tag : 111];
            //            }
            //            [_tableViewShow reloadData];
        }
    }];
}

- (void)testAlert
{
//    UIAlertView *ALERTINFO = [[UIAlertView alloc] initWithTitle:@"alert" message:@"test framework!" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"sure", nil];
//    [ALERTINFO show];
    
    NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
    [dic setObject:@"admin5@126.com" forKey:@"username"];
    [dic setObject:@"admin5" forKey:@"password"];
    
    [LoadingHelper showLoadingWithView:self.view];
    
    [LoginManager getUsersInfoWithOption:dic andBlock:^(NSDictionary *product, NSError *error) {
        
        [LoadingHelper hiddonLoadingWithView:self.view];
        
        [__rwTable headerEndRefreshing];
        
        NSLog(@"user_id:%@",[[product objectForKey:KEY_DATA] objectForKey:KEY_USERID]);
        NSLog(@"authority_id:%@",[[product objectForKey:KEY_DATA] objectForKey:KEY_AUTHORITYID]);
        //网络连接错误
        if (error) {
            NSInteger dic = [error code];
            //网络未连接
            if (dic == -1009) {
                UIAlertView *alert =[[UIAlertView alloc]initWithTitle:@"" message:@"网络未连接，暂时无法登录" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
                [alert show];
            }
            else {
                UIAlertView *alert =[[UIAlertView alloc]initWithTitle:@"" message:@"网络连接超时，请检查登录URL" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
                [alert show];
            }
        }
        //登录成功
        else if ([product objectForKey:KEY_DATA]) {
            UIAlertView *alert =[[UIAlertView alloc]initWithTitle:@"" message:@"登录成功" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
            [alert show];
        }
        //用户名或密码输入错误
        else{
            UIAlertView *alert =[[UIAlertView alloc]initWithTitle:@"" message:@"登录失败，账号或密码不正确" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
            [alert show];
        }
    }];
}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

@end
